﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Operator
{
    /// <summary>
    /// OperatorEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OperatorEditWindow : Window
    {
        private WarehouseService warehouseService = ServiceContainer.GetService<WarehouseService>();
        private OperatorService operatorService = ServiceContainer.GetService<OperatorService>();
        public Mengsk.ProxyDelivery.Domain.Operator Operator { get; set; }

        public OperatorEditWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Title = this.Operator == null ? "增加操作员" : "编辑操作员";

                if (this.Operator == null)
                {
                    this.Operator = new ProxyDelivery.Domain.Operator();
                    this.Operator.Enabled = true;
                }

                this.tbNumber.Text = this.Operator.Number;
                this.tbName.Text = this.Operator.Name;
                this.tbPhone.Text = this.Operator.Phone;
                this.chkEnabled.IsChecked = this.Operator.Enabled;
                var items = this.warehouseService.GetPageByMap(new Dictionary<string, object>(), 0, 0).Datas;
                this.cbbWarehouses.ItemsSource = items;
                var item = items.FirstOrDefault(obj => obj.Id == this.Operator.Warehouse);
                this.cbbWarehouses.SelectedItem = item;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = this.tbName.Text.Trim();
                string phone = this.tbPhone.Text.Trim();
                bool enabled = this.chkEnabled.IsChecked ?? false;
                var warehouse = this.cbbWarehouses.SelectedItem as Mengsk.ProxyDelivery.Domain.Warehouse;

                if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(phone) || warehouse == null)
                {
                    MessageBox.Show("请输入姓名,电话,仓库");
                    return;
                }

                this.Operator.Name = name;
                this.Operator.Phone = phone;
                this.Operator.Enabled = enabled;
                this.Operator.Warehouse = warehouse.Id;
                if (this.Operator.Id <1)
                {
                    this.Operator = this.operatorService.Insert(this.Operator);
                }
                else
                {
                    this.operatorService.Update(this.Operator);
                }
                MessageBox.Show("保存成功");
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
